<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-header">
        <div class="logo">
          <img src="../../../assets/vue.svg" />
        </div>
        <h2>免费注册</h2>
        <p class="register-desc">故事电影工具协作平台</p>
      </div>
      <el-form :model="registerForm" :rules="registerRules" ref="registerFormRef" class="register-form">
        <el-form-item prop="username">
          <el-input v-model="registerForm.userName" placeholder="用户名">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="company">
          <el-input v-model="registerForm.company" placeholder="公司名称">
            <template #prefix>
              <el-icon><OfficeBuilding /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input v-model="registerForm.email" placeholder="邮箱地址">
            <template #prefix>
              <el-icon><Message /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="密码" show-password>
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <div class="register-tip">
          点击"注册"即表示同意 <el-link type="primary" :underline="false">服务条款</el-link> 和 <el-link type="primary" :underline="false">隐私政策</el-link>。
        </div>
        <el-form-item>
          <el-button type="primary" @click="handleRegister" class="register-button">注册</el-button>
        </el-form-item>
      </el-form>
      <div class="register-footer">
        已有账号？<el-link :underline="false" @click="router.push('/login')">去登录</el-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { User, OfficeBuilding, Message, Lock } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { register } from '@/api/userApi'

const router = useRouter()
const registerForm = reactive({
  userName: '',
  company: '',
  email: '',
  password: ''
})
const registerRules = {
  userName: [
    { required: true, message: '请输入姓名', trigger: 'blur' }
  ],
  company: [
    { required: true, message: '请输入公司名称', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: ['blur', 'change'] }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' }
  ]
}
const registerFormRef = ref()

function handleRegister() {
  registerFormRef.value.validate(async (valid) => {
    if (valid) {
      const res = await register(registerForm)
      ElMessage.success('注册成功')
      router.push('/login')
    } else {
      ElMessage.error('请检查输入')
      return false
    }
  })
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: calc(100vh - 120px);
  background-color: #f7f8fa;
  padding-top: 80px;
  padding-bottom: 100px;
}
.register-box {
  background-color: white;
  padding: 2.5rem 3rem;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
  width: 100%;
  max-width: 400px;
  text-align: center;
}
.register-header {
  margin-bottom: 2rem;
}
.logo {
  margin-bottom: 1rem;
}
.register-header h2 {
  font-size: 2rem;
  font-weight: 700;
  color: #333;
}
.register-desc {
  color: #888;
  font-size: 1.1rem;
  margin-top: 8px;
}
.register-form {
  text-align: left;
}
.register-button {
  width: 100%;
  padding: 18px 20px;
  font-size: 1rem;
  height: 48px;
  border-radius: 8px;
  background: #6D5BFF;
  border-color: #6D5BFF;
}
.register-button:hover {
  background: #5a48e3;
  border-color: #5a48e3;
}
.register-tip {
  display: flex;
  justify-content: left;
  align-items: center;
  color: #888;
  font-size: 0.98rem;
  margin-bottom: 10px;
  margin-top: -8px;
  text-align: left;
}
.register-tip .el-link {
  color: #6D5BFF;
  font-weight: 500;
  font-size: 1rem;
}
.register-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 1.5rem;
  color: #888;
  font-size: 1rem;
}
.register-footer .el-link {
  color: #6D5BFF;
  font-weight: 500;
  font-size: 1rem;
}
.register-footer .el-link:hover {
  color: #5a48e3;
}
.register-extra {
  margin-top: 10px;
  color: #888;
  font-size: 0.95rem;
}
:deep(.el-input__inner) {
  height: 48px;
  line-height: 48px;
}
:deep(.el-input__prefix) {
  align-items: center;
  display: flex;
}
:deep(.el-input .el-input__icon) {
  font-size: 16px;
}
</style>